package com.lf.sp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lf.sp.common.ServerResponse;
import com.lf.sp.dao.CategoryMapper;
import com.lf.sp.pojo.Category;
import com.lf.sp.service.ICategoryService;

@Service("iCategoryService")
public class CategoryServiceImpl implements ICategoryService {
	private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

	@Autowired
	private CategoryMapper categoryMapper;
	
	@Override
	public ServerResponse addCategory(String categoryName , Integer parentId){
		//检查参数是否正确
		if(parentId == null || StringUtils.isBlank(categoryName)){
			return ServerResponse.createByErrorMessage("添加参数错误,请重新输入");
		}
		
		Category category = new Category();
		category.setName(categoryName);
		category.setParentId(parentId);
		category.setStatus(true);
		
		int resultCount = categoryMapper.insert(category);
		if(resultCount > 0){
			return ServerResponse.createBySuccess("添加成功");
		}
		return ServerResponse.createByErrorMessage("添加失败");
	}
	@Override
	public ServerResponse setCategoryName(Integer categoryId , String categoryName){
		//检查参数是否正确
		if(StringUtils.isBlank(categoryName) || categoryId == null){
			return ServerResponse.createByErrorMessage("输入的参数有误");
		}
		Category category = new Category();
		category.setId(categoryId);
		category.setName(categoryName);
		int resultCount = categoryMapper.updateByPrimaryKeySelective(category);
		if(resultCount > 0){
			return ServerResponse.createBySuccessMessage("更新成功");
		}
		return ServerResponse.createByErrorMessage("更新失败");
	}
	@Override
	public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
		if(categoryId == null){
			return ServerResponse.createByErrorMessage("参数错误");
		}
		List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
		if(CollectionUtils.isEmpty(categoryList)){
			logger.info("未找到该品类的子类");
		}
		return ServerResponse.createBySuccess(categoryList);
	}
	//<List<Category>>
	@Override
	public ServerResponse getCategoryAndDeepChildrenCategory(Integer categoryId){
		if(categoryId == null){
			return ServerResponse.createByErrorMessage("参数错误");
		}
		
		Set<Category> categoryss = findChildrenCategory(categorys, categoryId);
		Set<Integer> categoryIdSet = new HashSet<Integer>();
		for (Category category : categoryss) {
			categoryIdSet.add(category.getId());
		}
		return ServerResponse.createBySuccess( categoryIdSet);
	}
	
	private Set<Category> categorys = new HashSet<Category>();
	private Set<Category> findChildrenCategory( Set<Category>categorys, Integer categoryId ){
		List<Category> list =  categoryMapper.selectCategoryChildrenByParentId(categoryId);
		if(list.size() > 0){
			for (Category category : list) {
				categorys.add(category);
				findChildrenCategory(categorys, category.getId());
			}
		}
		return categorys;
	}
	
	
}
